// 这里用于定义通用的函数
// 这里的函数 调用的次数较多

var $ = function (name) { return document.getElementById(name) };
var c = function (name) { return document.createElement(name)  };

function get_time () {
    var d = new Date();
    return d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + " ";
}

function hide_panel(elem) {
    elem.parentElement.parentElement.style.display = "none";
}

function to_elem (name) {
    if (typeof name == 'string')
        return $(name);
    else if (typeof name == 'object')
        return name;
    else
        return 'unknown';
}
        

function hide_it (elem) {
    elem = to_elem(elem);
    elem.style.display = 'none';
}


function show_it (elem) {
    elem = to_elem(elem);
    elem.style.display = 'block';
    elem.style.zIndex  =  ++global_max_zindex;
}

// IE6 不支持数组的 indexOf 方法
if(!Array.prototype.indexOf){
    Array.prototype.indexOf = function(val){
       var value = this;
       for(var i =0; i < value.length; i++)
          if(value[i] == val) return i;
       return -1;
    };
}

// IE6 不支持字符串的 trim 函数, 摘自: http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie
if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}


// 格式化字符串, 源于ExtJS,摘自:http://witmax.cn/js-function-string-format.html
String.format = function(src){
    if (arguments.length == 0) return null;
    var args = Array.prototype.slice.call(arguments, 1);
    return src.replace(/\{(\d+)\}/g, function(m, i){
        return args[i];
    });
};


//新建一个xmlhttprequest,为Ajax作准备
function xhr (){
    var xmlhttp;
    if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    else // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    return xmlhttp;
}

//ajax 用get的方法发送消息
function xhr_get(call_back, url){
    var xmlhttp = xhr();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
            call_back(xmlhttp.responseText);
    }
    url = encodeURI(url);
    url = encodeURI(url);
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}

// 获取鼠标的坐标
var getMouseP=function (e){
	e = e || window.event;
	return (e.pageX || e.pageY)?{ x:e.pageX, y:e.pageY } : { x:e.clientX + document.body.scrollLeft - document.body.clientLeft, y:e.clientY + document.body.scrollTop  - document.body.clientTop };
};
    
move=function(o,t){
    o = (typeof o == 'string') ? $(o) : o;
    t = (typeof t == 'string') ? $(t) : t;
	o.onmousedown=function(ev){
		var mxy=getMouseP(ev);
		var by={x:mxy.x-(t.offsetLeft),y:mxy.y-(t.offsetTop)};
		o.style.cursor="move";
		document.onmousemove=function(ev){
			var mxy=getMouseP(ev);
			t.style.left=mxy.x-by.x+"px";
			t.style.top=mxy.y-by.y+"px";
		};
		document.onmouseup=function(){
			//window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
			this.onmousemove=null;
		}
	}
}

// 将文本转为json
function toJSON (s) {
    try { return eval( '(' + s + ')' ); }
    catch (e) { console.log (e); }
}

// 返回一个元素居中的位置
function middle (elem) {
    elem = to_elem (elem);
    var width = document.body.clientWidth;
    var height= document.body.clientHeight;
    var pos = {x: (width-elem.clientWidth)/2, y:(height-elem.clientHeight)/2};
    return pos;
}

// 将一个元素显示在中间
function show_middle (elem) {
    elem = to_elem(elem);
    show_it (elem);
    var pos = middle (elem);
    elem.style.position = "absolute";
    elem.style.top = pos.y + "px";
    elem.style.left= pos.x + "px";
}

// 显示|隐藏
function auto_show (elem) {
    elem = to_elem (elem);
    if (elem.clientWidth == 0 || elem.style.display=='none')
        show_it (elem);
    else
        hide_it (elem);
}

// 居中显示隐藏
function auto_show_middle (elem) {
    elem = to_elem (elem);
    if (elem.clientWidth == 0 || elem.style.display=='none')
        show_middle (elem);
    else
        hide_it (elem);
}
